<template>
  <div class="registerBox">
    <div class="userInpnut">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="name">
          <el-input
            type="text"
            maxlength="20"
            v-model="ruleForm.name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            maxlength="20"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            maxlength="20"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="注册权限" prop="power">
          <el-radio-group v-model="ruleForm.power">
            <el-radio label="普通用户"></el-radio>
            <el-radio label="管理员"></el-radio>
          </el-radio-group>
        </el-form-item>
        <div style="margin-left: 50px; width: 360px;">
          <router-link to="/login">
          <el-button
          type="primary"
          class="loginButton">登录</el-button>
        </router-link>
          <el-button
            type="success"
            class="loginButton"
            @click="submitForm('ruleForm', ruleForm)"
            >提交</el-button
          >
          <el-button
            @click="resetForm('ruleForm')"
            type="warning"
            class="loginButton"
            >重置</el-button
          >
          <router-link to="/airplane">
            <el-button class="loginButton">主页</el-button>
          </router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: "Register",
  data() {
    var vaildName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入用户名"));
      }
      setTimeout(() => {
        callback();
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        name: "",
        pass: "",
        checkPass: "",
        power: "",
      },
      rules: {
        name: [{ validator: vaildName, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        power: [
          {
            required: true,
            message: "请选择创建用户的权限",
            trigger: "change",
          },
        ],
      },
      tableData: 0,
      pageTotal: 1,
      pageSize: 10,
    };
  },
  methods: {
    submitForm(formName, form) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addUser(form);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 添加用户
    addUser(form) {
      var postForm = {
        name: form.name,
        password: form.pass,
        power: form.power === "普通用户" ? false : true,
      };
      this.$http
        .post("/server/user/addUser", postForm)
        .then((res) => {
          if (res.data.status) {
            this.$message({
              message: res.data.msg,
              type: "success",
            });
            this.$router.push("/login");
          } else {
            this.$message.error(res.data.msg);
          }
        });
    },
  },
};
</script>

<style>
.registerBox {
  height: 380px;
  width: 450px;
  border: 2px dashed #f69c55;
  margin: 200px auto;
  background-color: rgba(107, 206, 231, 0.8);
}
</style>
